package cn.tedu.file;

import java.io.*;

/**
 * @author: Dsi
 * @date: 2021/11/15 16:27
 * @description:该类用于测试字节输入流
 */
public class TestInputStream {

    public static void main(String[] args) {
        //method();
        method2();
    }

    private static void method2() {
        InputStream in=null;
        try {
            in =new FileInputStream("D:\\Ready\\1.txt");
            new File("D\\Ready\\2.txt").length();

            System.out.println();
            int a;
            while((a=in.read()) != -1){
                System.out.println(a);
            }

        } catch (Throwable e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    private static void method() {
        FileInputStream f2 = null;
        //InputStream i=new InputStream();//抽象父类不能被实例化
        //InputStream f=new FileInputStream("");
        try {
            FileInputStream f1 = new FileInputStream(new File("D:\\Ready\\1.txt"));
            f2 = new FileInputStream("D:\\Ready\\1.txt");
            //System.out.println(f2.read());
            //System.out.println(f2.read());
            //System.out.println(f2.read());
            //System.out.println(f2.read());
/*
            do{
                System.out.println(f2.read());
            }while(new File(String.valueOf(f2)).length()>0);

            //int c;
            *:数据流只能被读取一次，读取后消失
            while(f2.read()!=-1){
                System.out.println(f2.read());
            }
*/
            int b;
            while ((b = f2.read()) != -1) {
                System.out.println(b);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        /*finally代码块是Try，catch的第三个部分，无论是否捕捉到异常
        都会执行到这里来*/
        } finally {
            try {
                f2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
